package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.NewUserInfo;
import com.shaoshaohuo.app.view.NumberView;

import java.util.List;

//import android.support.v7.widget.RecyclerView;

/**
 * Created by Administrator on 2017/6/3.
 */

public class MyGridAdapter extends BaseAdapter {
    private List<Integer> picture_list;
    private List<String> word_list;
    private Context context;
    private NewUserInfo.Baseinfo baseUserInfoList;

    public MyGridAdapter(List<Integer> picture_list, List<String> word_list, Context context) {
        this.picture_list = picture_list;
        this.word_list = word_list;
        this.context = context;
    }

    public void setBaseUserInfoList(NewUserInfo.Baseinfo baseUserInfoList) {
        this.baseUserInfoList = baseUserInfoList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return word_list != null ? picture_list.size() : 0;
    }

    @Override
    public String getItem(int position) {
        return word_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        mViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.user_recyclerview_item, parent, false);
            holder = new mViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.user_picture_item);
            holder.textView = (TextView) convertView.findViewById(R.id.user_word_item);
            holder.numberView = (NumberView) convertView.findViewById(R.id.user_numberView);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.numberView.getLayoutParams();
            layoutParams.setMargins(0, 0, context.getResources().getDisplayMetrics().widthPixels / 16, 0);
            convertView.setTag(holder);
        } else {
            holder = (mViewHolder) convertView.getTag();
        }

        holder.textView.setText(word_list.get(position));
        holder.imageView.setImageDrawable(context.getResources().getDrawable(picture_list.get(position)));
        if (position > 3) {
            holder.numberView.setVisibility(View.GONE);
        } else {
            holder.numberView.setVisibility(View.GONE);

            if (baseUserInfoList != null) {
                NewUserInfo.Baseinfo baseinfo = baseUserInfoList;
                //待支付
                String waitPayNum = baseinfo.getWaitPayNum();
                //代发货
                String waitPushNum = baseinfo.getWaitPushNum();
                //待退款
                String waitBackMoneyNum = baseinfo.getWaitBackMoneyNum();
                //待收货
                String waitAcceptNum = baseinfo.getWaitAcceptNum();
                if (position == 0) {
                    changenum(holder.numberView, waitPayNum);
                } else if (position == 1) {
                    changenum(holder.numberView, waitPushNum);
                } else if (position == 2) {
                    changenum(holder.numberView, waitAcceptNum);
                } else if (position == 3) {
                    changenum(holder.numberView, waitBackMoneyNum);
                }


            }
        }
//        BaseUserInfo baseUserInfo = baseUserInfoList.get(0);
//        if (position == 0) {
//            if (baseUserInfo.getWaitPayNum().equals("0") && baseUserInfo.getWaitPayNum() == null) {
//                holder.numberView.setVisibility(View.GONE);
//            } else {
//                holder.numberView.setVisibility(View.VISIBLE);
//                holder.numberView.setNum(baseUserInfo.getWaitPayNum());
//            }
//        } else if (position == 1) {
//            if (baseUserInfo.getWaitPushNum().equals("0") && baseUserInfo.getWaitPushNum() == null) {
//                holder.numberView.setVisibility(View.GONE);
//            } else {
//                holder.numberView.setVisibility(View.VISIBLE);
//                holder.numberView.setNum(baseUserInfo.getWaitPushNum());
//            }
//        } else if (position == 2) {
//            if (baseUserInfo.getWaitAcceptNum().equals("0") && baseUserInfo.getWaitAcceptNum() == null) {
//                holder.numberView.setVisibility(View.GONE);
//            } else {
//                holder.numberView.setVisibility(View.VISIBLE);
//                holder.numberView.setNum(baseUserInfo.getWaitAcceptNum());
//            }
//        } else if (position == 3) {
//            if (baseUserInfo.getWaitBackMoneyNum().equals("0") && baseUserInfo.getWaitBackMoneyNum() == null) {
//                holder.numberView.setVisibility(View.GONE);
//            } else {
//                holder.numberView.setVisibility(View.VISIBLE);
//                holder.numberView.setNum(baseUserInfo.getWaitBackMoneyNum());
//            }
//        } else {
//            holder.numberView.setVisibility(View.GONE);
//        }

//        String waitAcceptNum = baseUserInfoList.get(0).getWaitAcceptNum();
        return convertView;
    }

    /**
     * 更改数据
     *
     * @param numberView
     * @param waitPayNum
     */
    private void changenum(NumberView numberView, String waitPayNum) {
        if (!waitPayNum.equals("0")) {
            numberView.setVisibility(View.VISIBLE);
            numberView.setNum(waitPayNum);
        } else {
            numberView.setVisibility(View.GONE);
        }
    }

    class mViewHolder {
        TextView textView;
        ImageView imageView;
        NumberView numberView;
    }
}
